package com.defysoft.waukids.calendar;

import android.opengl.Visibility;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.defysoft.waukids.R;

public class Calendar_Main_F extends Fragment implements OnClickListener{
	View view;
	TextView tv[] =new TextView[2];
	LinearLayout ly[] = new LinearLayout[2];
	FragmentManager fm;
	public Fragment [] fragment= new Fragment[2];
	ViewPager mPager;
	Calendar_PagerAdapter	mPagerAdapter;
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		view =inflater.inflate(R.layout.calendar_main_f, null);
		setStartData();
		return view;
	}
	
	public void setStartData(){
		fm= getChildFragmentManager();
		mPager = (ViewPager)view.findViewById(R.id.CALENDAR_MAIN_CONTAINER);
	
		fragment[0] = new Calendar_Main_Attend();
		fragment[1] = new Calendar_Main_Program();
		tv[0] = (TextView)view.findViewById(R.id.CALENDAR_MAIN_ATTEND);
		tv[0].setText("출석");
		tv[1] = (TextView)view.findViewById(R.id.CALENDAR_MAIN_PROGRAM);
		tv[1].setText("일정");
		ly[0] = (LinearLayout)view.findViewById(R.id.CALENDAR_MAIN_ATTEND_LL);
		ly[1] = (LinearLayout)view.findViewById(R.id.CALENDAR_MAIN_PROGRAM_LL);
		ly[0].setVisibility(View.VISIBLE);
		ly[1].setVisibility(View.GONE);
		tv[0].setOnClickListener(this);
		tv[1].setOnClickListener(this);
		mPagerAdapter = new Calendar_PagerAdapter(fm);
		mPager.setAdapter(mPagerAdapter);
		mPager.setOffscreenPageLimit(2);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					tv[0].setTextColor(0xff37b34a);
					tv[1].setTextColor(0xff8F8F8F);
					ly[0].setVisibility(View.VISIBLE);
					ly[1].setVisibility(View.GONE);
					((Calendar_Main_Attend)fragment[0]).setStartData();
					break;

				case 1:
					tv[1].setTextColor(0xff37b34a);
					tv[0].setTextColor(0xff8F8F8F);
					ly[1].setVisibility(View.VISIBLE);
					ly[0].setVisibility(View.GONE);
					((Calendar_Main_Program)fragment[1]).setStartData();
					break;
				}
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.CALENDAR_MAIN_ATTEND:
			tv[0].setTextColor(0xff37b34a);
			tv[1].setTextColor(0xff8F8F8F);
			ly[0].setVisibility(View.VISIBLE);
			ly[1].setVisibility(View.GONE);
			mPager.setCurrentItem(0);
			break;

		case R.id.CALENDAR_MAIN_PROGRAM:
			tv[1].setTextColor(0xff37b34a);
			tv[0].setTextColor(0xff8F8F8F);
			ly[1].setVisibility(View.VISIBLE);
			ly[0].setVisibility(View.GONE);
			mPager.setCurrentItem(1);		
			break;
		}
	}
	
	class Calendar_PagerAdapter extends FragmentPagerAdapter{

		public Calendar_PagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return fragment[arg0];
		}

		@Override
		public int getCount() {
			return fragment.length;
		}
		
	}
}
